package com.ggb.reactive.repository

import com.ggb.reactive.domain.entity.Dish
import org.springframework.data.r2dbc.repository.Modifying
import org.springframework.data.r2dbc.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.time.Instant


@Repository
interface DishRepository : ReactiveCrudRepository<Dish, Long> {

    //fun findAllByNameContainsAndStatusAndCategoryId(name: String,type:Int,CategoryId:Long): Flux<Dish>
    @Query("SELECT d FROM Dish d WHERE (:name IS NULL OR d.name LIKE %:name%) AND (:status IS NULL OR d.status = :status) AND (:categoryId IS NULL OR d.categoryId = :categoryId)")
    fun findDishesByOptionalCriteria(
        @Param("name") name: String?,
        @Param("status") type: Int?,
        @Param("categoryId") categoryId: Long?
    ): Flux<Dish>

    @Modifying
    @Query("UPDATE dish SET status = :status,update_time = :updateTime,update_user = :updateUser  WHERE id = :id")
    fun updateStatusById(id:Long, status:Int, updateTime: Instant, updateUser:Long): Mono<Long>
    fun findDishByCategoryId(categoryId: Long): Flux<Dish>
    fun findByIdIn(ids: List<Long>):Mono<Dish>


}